<?php
class Login extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('user_model', '', TRUE);
        $this->load->model('society_model', '', TRUE);
        $this->load->model('news_model', '', TRUE);
    }

	/**
	 * 按用户类别登录
	 * @param $category 分类名称
	 */
    function index($category = '') {
		$data['category'] = $category;
		$data['category_name'] = $this->_get_category_name($category);
		$data['html_title'] = $data['category_name'] . '登录';
		
		$data['current_menu_tab'] = MENU_TAB_A;
		
        //新闻推荐
		$condition['sort_view_count'] = DESC;
		$condition['offset'] = '';
		$condition['per_page'] = 8;
		$data['recommend_news'] = $this->news_model->search_by_condition($condition);
		
        //加载必须类
        $this->load->library('form_validation');
         
        //常规校验
        $this->form_validation->set_rules('name', '用户名', 'required|trim');
        $this->form_validation->set_rules('password', '密码', 'required|trim');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('frontend/login', $data);
            return;
        }

        //逻辑校验
        $this->form_validation->set_rules('login_error', '', 'callback_login_check');

        if ($this->form_validation->run() == FALSE) {
            $this->load->view('frontend/login', $data);
            return ;
        } else {
            switch ($category) {
                case SOCIETY:
                    redirect(BACKEND_SOCIETY_PATH);
                    break;
            	case ASSOCIATION:
                    redirect(BACKEND_ASSOCIATION_PATH);
                    break;
                case TEACHER:
                    redirect(BACKEND_TEACHER_PATH);
                    break;
            }
        }
    }

    function _get_category_id($category) {
        switch ($category) {
            case SOCIETY:
                return SOCIETY_UCID;
            case ASSOCIATION:
                return ASSOCIATION_UCID;
            case TEACHER:
            	return TEACHER_UCID;
            default:
            	show_404();
            	return ;
        }
    }

    function _get_category_name($category) {
        switch ($category) {
            case SOCIETY:
                return SOCIETY_CHS;
            case ASSOCIATION:
                return ASSOCIATION_CHS;
			case TEACHER:
            	return TEACHER_CHS;
            default:
            	show_404();
            	return ;
        }
    }
    
    function login_check() {
        $category = $this->uri->segment(4);
        $category_id = $this->_get_category_id($category);
        
        $name = $this->input->post("name");
        $password = $this->input->post("password");
        $data = $this->user_model->get_user_by_name_and_category($name, $category_id);
        
        if ($data) {
            if ($password != $data['password']) {
                $this->form_validation->set_message('login_check', '用户名或密码错误');
                return FALSE;
            } else {
                $this->_allocate_user_auth_session($data);
                return TRUE;
            }
        } else {
            $this->form_validation->set_message('login_check', '该用户不存在');
            return FALSE;
        }
    }
    
	function logout() {
		$this->session->sess_destroy();
		session_destroy();
		redirect(base_url());
	}
 
}